<template>
  <div class="register">
    <van-icon
      name="music-o"
      size="50vw"
      color="rgb(219, 65, 65)"
      style="padding: 10vw 0;"
    />
    <van-form @submit="onSubmit">
      <van-field
        type="tel"
        v-model="phoneNumber"
        name="phoneNumber"
        label="手机号"
        placeholder="请输入手机号"
        autofocus="true"
        :rules="[{ required: true }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="输入不少于6位密码"
        :rules="[{ required: true }]"
      />

      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          @click="toCheckCode"
        >
          注册
        </van-button>
      </div>
    </van-form>
    <van-button type="default" style="border: none" :to="{ name: 'login' }"
      >已有账号，立即登录</van-button
    >
    <h1 class="title">播放器</h1>
  </div>
</template>

<script>
import { Dialog } from "vant";
import axios from "axios";
export default {
  data() {
    return {
      phoneNumber: "",
      password: "",
    };
  },
  methods: {
    validPhoneNumber() {
      return /^1[35789]\d{9}$/.test(this.phoneNumber);
    },
    validPassword() {
      return this.password.length > 6;
    },
    toCheckCode() {
      if (this.validPhoneNumber() && this.validPassword()) {
        axios
          .get(
            "http://localhost:3000/cellphone/existence/check?phone=" +
              this.phoneNumber
          )
          .then((res) => {
            if (res.data.exist != -1) {
              Dialog.alert({
                message: "该手机号已注册，请登录！",
              });
              this.$router.push({
                name: "login",
                query: { phoneNumber: this.phoneNumber },
              });
            } else {
              axios
                .get(
                  "http://localhost:3000/captcha/sent?phone=" + this.phoneNumber
                )
                .then((res) => {
                  console.log(res);
                });
              this.$router.push({
                name: "checkCode",
                query: {
                  phoneNumber: this.phoneNumber,
                  password: this.password,
                },
              });
            }
          });
      } else {
        this.password = "";
        Dialog.alert({
          message: "请输入正确的手机号和密码",
        });
      }
    },
    onSubmit(values) {
      console.log("submit", values);
    },
  },
};
</script>

<style scoped>
.register {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.register .van-button--info {
  background: rgb(235, 30, 30);
  border: rgb(235, 30, 30);
}
.title {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  font-family: "华文行楷";
  font-weight: normal;
  color: rgb(126, 122, 121);
  font-size: 22px;
  line-height: 40px;
  text-align: center;
}
</style>
